package com.powernode.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Security 安全框架认识的安全user对象
 *
 * @Author: DongGuoZhen
 * @Date: 2025/09/22/09:28
 * @Description:
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SecurityUser implements UserDetails {

    //    商城后台管理系统用户的相关属性
    private Long userId;
    private String username;
    private String password;
    private Integer status;
    private Long shopId;
    private String loginType;
    private Set<String> perms = new HashSet<>();


//    商城购物系统会员的相关属性


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.loginType + this.userId;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }

    public Set<String> getPerms() {
        HashSet<String> finalPermsSet = new HashSet<>();
        perms.forEach(perms -> {
//            判断是否包含,
            if (perms.contains(",")) {
//                 一条权限中有多个权限
//                根据,进行分割
                String[] realPerms = perms.split(",");
                for (String realPerm : realPerms) {
                    finalPermsSet.add(realPerm);
                }
            } else {
//                一条权限
                finalPermsSet.add(perms);
            }
        });
        return finalPermsSet;
    }
}
